<?php

namespace app\common\controller;

use app\common\library\Auth;
use app\common\model\Custom as CustomModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Route;


class Frontend extends Api
{
    /**
     * 需登录的方法
     * 访问本控制器的此方法，无需会员登录
     */
    protected $noNeedLogin = [];
    /**
     * 权限类实例
     * @var Auth
     */
    protected $auth = null;

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 微信授权
     * @param $activityId
     * @param string $flag
     * @return void
     */
    public function getWeChatAuth($activityId, string $flag = '')
    {
        $appid        = get_sys_config("appid");
        $redirect_uri = Route::buildUrl('api/WeChat/auth')->domain(true) . '?activity_id=' . $activityId . '&flag=' . $flag;
        $state        = '';
        if (isset($_SERVER)) {
            $state = $_SERVER['HTTP_REFERER'] ?? '';
            $state = getJumpUrl($state, ['token']);
        }
        $url             = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($redirect_uri) . '&response_type=code&scope=snsapi_userinfo&state=' . $state . '#wechat_redirect';
        $tokenName       = Config::get("backstage.user_token_name");
        $serverTokenName = Config::get("backstage.user_server_token_name");
        $token           = $this->request->server($serverTokenName, $this->request->request($tokenName, Cookie::get($tokenName) ?: false));
        if (empty($token)) {
            $this->error("", ['url' => $url], 302);
        }
        try {
            $this->auth->init($token);
            if (!$this->auth->isLogin()) {
                $this->error("", ['url' => $url], 302);
            }
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            $this->error("授权失败！");
        }
    }

    /**
     * 微信授权
     * @param $activityId
     * @param string $path
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getWeChatAuth_v1($activityId, string $path = '')
    {
        $appid        = get_sys_config("appid");
        $redirect_uri = Route::buildUrl('api/WeChat/auth')->domain(true) . '?activity_id=' . $activityId;
        if (empty($path)) {
            $state = 'index_' . $activityId;
        } else {
            $state = $path;
        }
        $url             = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=' . $state . '#wechat_redirect';
        $tokenName       = Config::get("backstage.user_token_name");
        $serverTokenName = Config::get("backstage.user_server_token_name");
        $token           = $this->request->server($serverTokenName, $this->request->request($tokenName, Cookie::get($tokenName) ?: false));
        if (empty($token)) {
            $this->error("", ['url' => $url], 302);
        }
        $this->auth->init($token);
        if (!$this->auth->isLogin()) {
            $this->error("", ['url' => $url], 302);
        }
    }

}
